<?php

namespace App\Filament\Resources\ConfigItemResource\Pages;

use App\Filament\Resources\ConfigItemResource;
use App\Models\Config;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;
use Filament\Resources\Components\Tab;
use Illuminate\Database\Eloquent\Builder;

class ListConfigItems extends ListRecords
{
    protected static string $resource = ConfigItemResource::class;

    protected function getHeaderActions(): array
    {
        return [
            Actions\CreateAction::make(),
        ];
    }

    public function getTabs(): array
    {
        $tabs = [
            __('config-item.All') => Tab::make(),
        ];
        $configs = $this->getConfigs();
        foreach ($configs as $config) {
            $tabs[$config->name] = Tab::make()
                ->modifyQueryUsing(fn (Builder $query) => $query->where('config_id', $config->id));
        }
        return $tabs;
    }

    public function getConfigs()
    {
        return Config::query()->get();
    }
}
